package br.mestrado.unb.bs.sensor;

import br.mestrado.unb.bs.communication.IBluetoothWriter;
import br.mestrado.unb.bs.model.configuration.SensorConfiguration;
import br.mestrado.unb.bs.model.configuration.SensorIdentification;
import br.mestrado.unb.bs.util.Log;
import br.mestrado.unb.bs.view.ISensorWindow;

public abstract class ThreadSensor extends Thread{
	
	protected IBluetoothWriter bluetooth;
	protected SensorIdentification id;
	protected ISensorWindow window;
	
	
	protected SensorConfiguration config = null;
	protected float period;
	
	public ThreadSensor(SensorConfiguration config, SensorIdentification id, IBluetoothWriter bluetooth) {
		this.config = config;
		this.id = id;
		this.period = (float) 2000;//DOIS MINUTOS
		this.bluetooth = bluetooth;
	}
	
	@Override
	public void run() {
		while(true){
			try{
				this.sendValue();
			}catch (Exception e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(new Float(period).longValue());
			} catch (InterruptedException e) {
				window.addMessage(this.getName() + ": Sensor interrompido.");
				Log.w(this.getName(),"Thread Interrompida");
				break;
			}
		}
	}

	public void setSensorWindow(ISensorWindow sensorWindow) {
		this.window = sensorWindow;
	}
	
	protected abstract void sendValue() throws Exception;
}
